(function($) {
	$.fn.jobsel = function(options) {//options 经常用这个表示有许多个参数。 
	    //定义jobs的构造函数
	    var jobs = function(ele, opt) {
	        this.$element = ele,
	        this.defaults = {
	            'size': 3,
	            'data':{},
	            'html':{},
	            'target':"",
	            'inputName':"",
	            'showsub':"",
	            'zindex':99999
	        },
	        this.options = $.extend({}, this.defaults, opt)
	    }
		 
	    //加载城市数据
	    var jobsdata = function (o){
	    	var result = null;
	    	$.ajax({
			    type: "GET",
			    url: "/js/jobsel/jobs.json",
			    async:false,
			    dataType: "json",
			    success: function(data){
			    	 result = data;
			    	 o.options.data=result; 
			    	 o.showcontent()
			    } 
			});
	    
	    }
	    
	    
	  //加载选中数据
	    var seldata= function (o){
	    	var html = "";
	    	var html1 = "";
	    	var ids =  $("#"+o.options.inputName).val(); 
	    	var names ="";
	    	var showname="";
	    	var parentname ="";
	        if(ids!=null&&ids.length>0){
	        	var idArray = ids.split(",");
	        	
	        	for(var i=0;i<idArray.length;i++){
	        		var data = o.options.data;
	        		var parentid = idArray[i].substring(0,2)+"000";
	        		
	        		 oneloop:
	        		 for(var j = 0 ;j<data.length;j++){
    				   var subdata = data[j].sublist;
    				   //获取父节点名称
    				   if(parentname.length<1){
    					   for(var f=0;f<data.length;f++){ 
       					    if(parentid==data[f].value){
       					    	parentname =data[f].name+"-";
       					    	break;
       					    } 
       				      }
    				   }
    				 
    				   
    				   for(var k=0;k<subdata.length;k++){
    					   if(idArray[i]==subdata[k].value){
							   if(names.length>0){
	        					  names+=","
	        				  }
	        				  names +=subdata[k].name;
	        				  if(showname.length>1){
	        					  showname +=","
	        				  }
	        				  showname  +=names;
							   break oneloop;
    					   }else{
    						   var subdatatwo = subdata[k].sublist;
    						   if(subdatatwo.length>0){
    							   for(var l=0;l<subdatatwo.length;l++){
	        						  
	        						   if(idArray[i]==subdatatwo[l].value){
	        							    
	        							   if(names.length>0){
	 			        					  names+=","
	 			        				  }
	 			        				  names +=subdatatwo[l].name;
	 			        				  if(showname.length>1){
	 			        					  showname +=","
			 			        				  }
			 			        				  showname  +=names;
			        							   break oneloop;
				        					   }
				        				   } 
	        						   }
	        					   }    
	        				   } 
			        		 }
			        	} 
			        }
			        o.$element.val(parentname+showname);
			      return  {html:html,html1:html1};
			    }
 
			    /**
	     * type 0 热门城市 1 省份  2 标签卡   3城市  
	     */
	    var alldata = function (o,type,pid,selid){
	     
	    	var html = "";
	    	if(type==1){
	    		 var category = o.options.data; 
	    		 var sub = o.options.showsub;
    			 if(sub.length>0){
    				 for(var i =0 ;i<category.length;i++){
    					 if(category[i].value==sub){
	    					 var categorytwo =category[i].sublist; 
				    		 for(var j =0 ;j<categorytwo.length;j++){
				    			 html +='<span  data-code="'+categorytwo[j].value+'" data-id="'+categorytwo[j].id+'"   data-pid="'+categorytwo[j].pid+'" >'+categorytwo[j].name+'</span>' 
				    		 }
	    					break; 
	    				 }
    				 } 
    			 }else{
    				 for(var i =0 ;i<category.length;i++){
    					 if(category[i].value!="20000"){
    						 html +='<span  data-code="'+category[i].value+'" data-id="'+category[i].id+'"   data-pid="'+category[i].pid+'" >'+category[i].name+'</span>' 
    					 } 
		    		 }
    			 }   
	    	 }else  {
	    		 var data = o.options.data;   
	    		 for(var j = 0 ;j<data.length;j++){
	    			 var sub = o.options.showsub;
	    			 if(sub.length>0){
	    				    var sublist = data[j].sublist;   
			   				for(var i =0 ;i<sublist.length;i++){
				   				  if(sublist[i].id== pid){
				   					var name = sublist[i].name;
				   					var subdatatwo = sublist[i].sublist;
	        						if(subdatatwo.length>0){
        							   for(var l=0;l<subdatatwo.length;l++){
        								   name = subdatatwo[l].name;
        								   if(name.length>6){
        					   					name = name.substring(0,9)+"...";
        					   			   }
        					   			   html +=' <span data-code="'+subdatatwo[l].value+'" data-id="'+subdatatwo[l].id+'"   data-pid="'+subdatatwo[l].pid+'"  title="'+subdatatwo[l].name+'"    alt="'+subdatatwo[l].name+'"   >'+name+'</span> ';	
        							   }
	        						}else{
	        							 if(name.length>6){
	     				   					name = name.substring(0,9)+"...";
	     				   				   }
	     				   				   html +=' <span data-code="'+sublist[i].value+'" data-id="'+sublist[i].id+'"   data-pid="'+sublist[i].pid+'"  title="'+sublist[i].name+'"    alt="'+sublist[i].name+'"   >'+name+'</span> ';	
	        						}
				   				  } 
			   				}
	    			 }else{
	    				   if(data[j].id== pid){
			   				   var sublist = data[j].sublist;   
				   				for(var i =0 ;i<sublist.length;i++){
				   					var name = sublist[i].name;
				   					var subdatatwo = sublist[i].sublist;
	        						if(subdatatwo.length>0){
        							   for(var l=0;l<subdatatwo.length;l++){
        								   name = subdatatwo[l].name;
        								   if(name.length>6){
        					   					name = name.substring(0,9)+"...";
        					   			   }
        					   			   html +=' <span data-code="'+subdatatwo[l].value+'" data-id="'+subdatatwo[l].id+'"   data-pid="'+subdatatwo[l].pid+'"  title="'+subdatatwo[l].name+'"    alt="'+subdatatwo[l].name+'"   >'+name+'</span> ';	
        							   }
	        						}else{
	        							 if(name.length>6){
	     				   					name = name.substring(0,9)+"...";
	     				   				   }
	     				   				   html +=' <span data-code="'+sublist[i].value+'" data-id="'+sublist[i].id+'"   data-pid="'+sublist[i].pid+'"  title="'+sublist[i].name+'"    alt="'+sublist[i].name+'"   >'+name+'</span> ';	
	        						}
				   					 
				   				  
				   				} 
			   			   } 
	    			 }
	    			 
	    			 
	   			
	    		 }
	    	 } 
	     
	    	return html;
	    }
	    
	    
	  //加载页面显示
	    var jobshtml = function(o){
	    	var html =new Array();
	    	   var selObj = seldata(o); 
	    	   html +=' <div class="fn-hide '+o.options.inputName+'Pop" style="display:none;">'
	    	   var sub = o.options.showsub;
        	   if(sub.length>0){
        		   html +='  <div class="addrs-wrapper">'
        	   }else{
        		   html +='  <div class="addrs-wrapper wrapper-sizeReset">'
        	   }  
               html +='    <div class="optionsList">'
               html +='          <ul class="clefix">'
               html +='			    	                <li class="allprovin"><span>全部 </span></li>'
               html +='	    	                <li class="province"> <span class="provinTab"></span> <span class="icon"></span></li>'
               html +='      </ul>'
               html +='  </div>'
               html +='<div class="types-inner addrs-spanwrapper">' 
               html +='<div class="common-types">'
               html +=' <div class="clefix">'
               html +='   <div class="city-lists">'
               html +=alldata(o,1)	    
               html +='      </div>'
               html +='   </div>'
               html +=' </div> ' 
			   html +='</div>'
			   html +=' <div class="types-hide addrs-spanwrapper">'
			   html +='<div class="citysOptions">'
			   html +='<div class="hots">'
			   html +='<h5 class="typeUp"></h5>'
	    	   html +=' <div class="typeList typesInside">'
	    	   html +='   <span>XXX</span>'
	    	   html +=' </div>'
	    	   html +=' </div>'
	    	   html +='</div>'
	    	   html +=' </div>'
	    	   html +='</div>'
	    	   html +='</div>'
               o.options.html =html;
	    }
	     
	  //定义jobs的方法
	    jobs.prototype = {
	        init: function() {
	               jobsdata(this); 
	              
	        },
	     selData:function(){
	    	 seldata(this); 
	     }
	    ,showcontent:function(){
	        	 jobshtml(this); 
	               var o = this;
	               var popObj =null;
	               var jobHtmlDt = this.options.html;
	               var thisobj = '.'+this.options.inputName+'Pop';
	               $("body").append(jobHtmlDt)
	               o.$element.on("click",function(){ 
	            	   var array=['890px','380px'];
	            	   var sub = o.options.showsub;
	            	   if(sub.length>0){
	            		   array=['890px','380px'];
	            	   }
	            	   popObj  = layer.open({
	                       type:1,
	                       zIndex: o.options.zindex,
	                        title:"职称选择",
	                        shadeClose:true,
	                        content:$(thisobj),
	                        area:array
	                    }); 
	               });
	               
	               //省份选择 
	               $(document).on('click','.'+o.options.inputName+'Pop .common-types span',function(){
	                   var indexjob = $('.'+o.options.inputName+'Pop .common-types span').index($(this)),
	                       provinVal = $(this).text(),
	                       id  = $(this).attr("data-id"),listHtml =  alldata(o,3,id,0);
	                       $('.types-inner span').removeClass('active').eq(indexjob).addClass('active');
	                   var sub = o.options.showsub;
	                   //为职称选择时
	            	   if(sub.length>0){
	            		   o.$element.val(provinVal);
			           	   $("#"+o.options.inputName).val($(this).attr("data-code")); 
			           	 layer.close(popObj);
	            	   }else{
	            		  
		                   $('.'+o.options.inputName+'Pop .types-inner').hide();
		                   $('.'+o.options.inputName+'Pop .types-hide').show();
		                   $('.'+o.options.inputName+'Pop .province').show().text(provinVal);
		                   $('.'+o.options.inputName+'Pop .provinUp').text(provinVal);
		                   $('.'+o.options.inputName+'Pop .typesInside').html(listHtml)
	            	   }
	                   
	                  
	               });
	               //全部
	               $(document).on('click','.'+o.options.inputName+'Pop .allprovin',function(){
	                   $(this).next().hide();
	                   $('.'+o.options.inputName+'Pop .types-hide').hide();
	                   $('.'+o.options.inputName+'Pop .types-inner').show();
	               });

	               //确定城市选择
	               $(document).on('click','.'+o.options.inputName+'Pop .typesInside span',function(){
	                   var jobVal = $(this).attr("title");
	              	   var code = $(this).attr("data-code");
	              	 var pid = $(this).attr("data-pid");
	              	 
	               var text =	$('.'+o.options.inputName+'Pop .city-lists span[data-id='+pid+']').text();
	              	 
	                   o.$element.val(text+'-'+jobVal);
		           	   $("#"+o.options.inputName).val(code); 
	                   layer.close(popObj);
	               });
	        	
	        }
	    }
	    var jobobj = new jobs(this, options);

		return  jobobj;
	}
 
})(jQuery);
